package com.zego.wawaji.ui.start;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.third.ThirdConstant;
import com.zego.wawaji.third.auth.OauthBack;
import com.zego.wawaji.ui.UIShow;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.dialog.LoadingDialog;
import com.zego.wawaji.wawaji_client.util.OnSoundClickListener;

/**
 * 注册登陆页面
 */
public class NavUserAct extends BaseAppCompatActivity {
    private OauthBack oauthBack;
    private ImageView login_wx, login_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isCanBack(false);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.p1_nav_act);
        initView();
        initCallback();
        initData();
    }

    private void initCallback() {
        if (oauthBack == null) {
            oauthBack = new OauthBack(this);
            ModuleMgr.getAuthMgr().setCallback(oauthBack);
        }
    }

    private void initData() {
        ModuleMgr.getWawaMgr().reqSetting(null);
    }

    private void initView() {
        login_wx = (ImageView) findViewById(R.id.login_wx);
        login_phone = (ImageView) findViewById(R.id.login_phone);
        login_wx.setOnClickListener(soundClickListener);
        login_phone.setOnClickListener(soundClickListener);
    }

    private OnSoundClickListener soundClickListener = new OnSoundClickListener() {
        @Override
        public void onSoundClick(View v) {
            initCallback();
            switch (v.getId()) {
                case R.id.login_phone:
                    login_phone.setEnabled(false);
                    UIShow.showRegMobileAct(NavUserAct.this);
                    login_phone.setEnabled(true);
                    break;

                case R.id.login_wx:
                    login_wx.setEnabled(false);
                    LoadingDialog.show(NavUserAct.this, "请稍候");
                    ModuleMgr.getAuthMgr().onLogin(ThirdConstant.WECHAT);
                    login_phone.setEnabled(true);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        login_wx.setEnabled(true);
        login_phone.setEnabled(true);
    }

    @Override
    public void onBackPressed() {
        ModuleMgr.getLoginMgr().clearCookie();
        super.onBackPressed();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (ThirdConstant.mPlatform == ThirdConstant.WECHAT)
            LoadingDialog.closeLoadingDialog();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ModuleMgr.getAuthMgr().release();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // QQ登录回调
        ModuleMgr.getAuthMgr().onActivityResult(requestCode, resultCode, data);
    }
}
